<?php
class CustomTeamMemberField extends TeamMemberField {
	private static $allowed_actions = array(
        'check_team'
    );
	
	function validate($validator) {
		$valid = parent::validate($validator);
		
		if($valid){
			$current_member = $this->getForm()->Controller()->CurrentMember();
			$member = Member::get()->find('Username', $this->Value());
			if($member->RegisterType == 'WithoutPayment' && !$current_member->AllowUpgradeWithoutPayment){
				$valid = false;
				$validator->validationError($this->name, _t('TeamMemberField.USERNAME_INVALID'));
			}
		}
		
		return $valid;
    }
	
	/**
     * Handle a request for an team member checking.
     * 
     * @param HTTPRequest $request The request to handle.
     * @return Team member data result
     */
    function check_team(HTTPRequest $request) {
    	$data = array('result' => 'error', 'id' => '', 'name' => '', 'rank' => '', 'msg' => _t('TeamMemberField.USERNAME_INVALID'));

		$current_member = $this->getForm()->Controller()->CurrentMember();
		$member = Member::get()->find('Username', $request->getVar('username'));
		if($member->RegisterType == 'WithoutPayment' && !$current_member->AllowUpgradeWithoutPayment){
			return Convert::array2json($data);
		}
			
		$sponsor = Sponsor::get()->find('MemberID', $this->getMemberID());
		$member_id = Distributor::get_id_by_username($request->getVar('username'));
    	if($this->getIncludeOwnself()){
	    	$result = Sponsor::get()
	    	->filter('NLeft:GreaterThanOrEqual', (int)$sponsor->NLeft)
	    	->filter('NRight:LessThanOrEqual', (int)$sponsor->NRight)
			->filter('MemberID', (int)$member_id)->count();
			
			if(!$result){
				$placement = Placement::get()->find('MemberID', $this->getMemberID());
				$result = Placement::get()
		    	->filter('NLeft:GreaterThanOrEqual', (int)$placement->NLeft)
		    	->filter('NRight:LessThanOrEqual', (int)$placement->NRight)
				->filter('MemberID', (int)$member_id)->count();
			}
		} else {
			$result = Sponsor::get()
	    	->filter('NLeft:GreaterThan', (int)$sponsor->NLeft)
	    	->filter('NRight:LessThan', (int)$sponsor->NRight)
			->filter('MemberID', (int)$member_id)->count();
			
			if(!$result){
				$placement = Placement::get()->find('MemberID', $this->getMemberID());
				$result = Placement::get()
		    	->filter('NLeft:GreaterThan', (int)$placement->NLeft)
		    	->filter('NRight:LessThan', (int)$placement->NRight)
				->filter('MemberID', (int)$member_id)->count();
			}
		}
		
		if($this->getIncludeUpline()){
			$upline_result = Sponsor::get()
	    	->filter('NLeft:LessThan', (int)$sponsor->NLeft)
	    	->filter('NRight:GreaterThan', (int)$sponsor->NRight)
			->filter('MemberID', (int)$member_id)->count();
			
			if(!$upline_result){
				$placement = Placement::get()->find('MemberID', $this->getMemberID());
				$upline_result = Placement::get()
		    	->filter('NLeft:LessThan', (int)$placement->NLeft)
		    	->filter('NRight:GreaterThan', (int)$placement->NRight)
				->filter('MemberID', (int)$member_id)->count();
			}
			
	 		if($result || $upline_result) {
	 			$member = Distributor::get_obj_by_username($request->getVar('username'));
	            $data = array('result' => 'success', 'id' => $member->ID, 'name' => $member->getName(), 'rank' => $member->getRankTitle(), 'msg' => _t('TeamMemberField.USERNAME_VALID'));
	        }
 		} else {
 			if($result) {
 				$member = Distributor::get_obj_by_username($request->getVar('username'));
 				$data = array('result' => 'success', 'id' => $member->ID, 'name' => $member->getName(), 'rank' => $member->getRankTitle(), 'msg' => _t('TeamMemberField.USERNAME_VALID'));
			}
 		}

        // the response body
        return Convert::array2json($data);
    }
}
